home *** CD-ROM | disk | FTP | other *** search
- #include <ToolboxTools.h>
- #include <OSUtils.h> // for GetDateTime
- #include <BDC.h> // for ToString
- #include <Menus.h> // for SetItem
-
- void ToString2 (long tempInt, register StringPtr tempString);
-
- #define kChooserName -16096
-
- /*------------------------------------------------------------------------------*/
-
- StringPtr GetChooserName (StringPtr Default)
- /* return the name which the chooser contains as the user name */
- {
- StringHandle ChooserName;
-
- ChooserName = (StringHandle)GetResource ('STR ', kChooserName);
- if (!ChooserName)
- return Default;
- else
- return *ChooserName;
- }
-
- /*------------------------------------------------------------------------------*/
-
- Boolean ButtonStillDownAfterN (long NumTicks)
- /* return TRUE if the mouse button is held down for longer than NumTicks */
- /* 60'ths of a second; false if it is released before that */
- {
- long StartTime;
- StartTime = TickCount();
-
- while (StillDown())
- if (TickCount() - StartTime > NumTicks)
- return TRUE;
- return FALSE;
- }
-
- /*------------------------------------------------------------------------------*/
-
- Boolean CommandKeyDown (short modifierKeys)
- {
- if (modifierKeys & 0x0100)
- return TRUE;
- else
- return FALSE;
- }
-
- /*------------------------------------------------------------------------------*/
-
- Boolean ShiftKeyDown (short modifierKeys)
- {
- if (modifierKeys & 0x0200)
- return TRUE;
- else
- return FALSE;
- }
-
- /*------------------------------------------------------------------------------*/
-
- Boolean OptionKeyDown (short modifierKeys)
- {
- if (modifierKeys & 0x0800)
- return TRUE;
- else
- return FALSE;
- }
-
- /*------------------------------------------------------------------------------*/
-
- Boolean ControlKeyDown (short modifierKeys)
- {
- if (modifierKeys & 0x1000)
- return TRUE;
- else
- return FALSE;
- }
-
- /*------------------------------------------------------------------------------*/
-
- short isPressed(unsigned short k )
- // k = any keyboard scan code, 0-127
- {
- unsigned char km[16];
-
- GetKeys( (long *) km);
- return ( ( km[k>>3] >> (k & 7) ) & 1);
- }
-
- /*------------------------------------------------------------------------------*/
-
- short getModifiers (void)
- /* get the states of all the modifier keys on the keyboard and return them */
- /* in ModKeys in the format used in the 'modifier' field of an event record */
- {
- short ModKeys;
-
- ModKeys = 0;
- if (isPressed(0x37)) // command key
- ModKeys += 0x0100;
- if (isPressed(0x38)) // shift key
- ModKeys += 0x0200;
- if (isPressed(0x39)) // caps-lock key
- ModKeys += 0x0400;
- if (isPressed(0x3A)) // option key
- ModKeys += 0x0800;
- if (isPressed(0x3B)) // control key
- ModKeys += 0x1000;
- return ModKeys;
- }
-
- /*------------------------------------------------------------------------------*/
-
- void ErrorAlert (short ErrorID, StringPtr Message)
- /* this routine puts up a dialog which displays an error in one of the
- following ways:
- 1) if there is an STR resource with the same ID as ErrorID, that
- message is displayed along with the passed-in message
- 2) if there is no STR resource, the ErrorID and the passed-in
- message are displayed.
- */
- {
- Handle ErrorMsg = Get1Resource ('STR ', ErrorID);
- Str255 STemp, STemp2;
-
- if (ErrorMsg)
- {
- if (Message)
- PStrCat (254, STemp, 3, (StringPtr)(*ErrorMsg), "\p\r", Message);
- else
- BlockMove (*ErrorMsg, STemp, (**ErrorMsg)+1);
- }
- else
- {
- NumToString (ErrorID, (StringPtr)(&STemp2));
- if (Message)
- PStrCat (254, STemp, 4, "\pError # ", STemp2, "\p : \r", Message);
- else
- PStrCat (254, STemp, 2, "\pError # ", STemp2);
- }
-
- ParamText (STemp, NULL, NULL, NULL);
- NoteAlert (-16394, NULL); // use a system alert to show the message
- }
-
- /*------------------------------------------------------------------------------*/
-
- void TimeString (long inDateTime, Boolean showAMPM, StringPtr DestString, short maxLen)
- {
- DateTimeRec tempDate;
- unsigned char min[3], hr[3], sec[3];
- Str255 STemp;
- char C = 0;
-
- Secs2Date (inDateTime, &tempDate);
- if (showAMPM)
- {
- if (tempDate.hour == 0)
- {
- tempDate.hour = 12;
- C = 'A';
- }
- else if ((tempDate.hour >= 13) && (tempDate.hour <= 23))
- {
- tempDate.hour = tempDate.hour - 12;
- C = 'P';
- }
- else
- C = 'A';
- }
-
- ToString2(tempDate.hour, hr);
- ToString2(tempDate.minute, min);
- ToString2(tempDate.second, sec);
-
- // Construct the basic time string
- PStrCat (255, DestString, 5, hr, "\p:", min, "\p:", sec);
-
- // add 'AM' or 'PM' as needed
- switch (C) {
- case 'A' : PStrCat (255, DestString, 2, DestString, "\p AM"); break;
- case 'P' : PStrCat (255, DestString, 2, DestString, "\p PM"); break;
- }
- }
-
- /*------------------------------------------------------------------------------*/
-
- void CurrentTimeString (Boolean showAMPM, StringPtr DestString, short maxLen)
- {
- unsigned long InDateTime;
-
- GetDateTime(&InDateTime);
- TimeString(InDateTime, showAMPM, DestString, maxLen);
- }
-
- /*------------------------------------------------------------------------------*/
-
- void ToString2 (long tempInt, register StringPtr tempString)
- /* a special routine which takes an integer, converts it to a string */
- /* and then, pads it with zeroes on the left or truncates characters on */
- /* the right to make sure it is exactly 2 characters long */
- {
- NumToString(tempInt, tempString);
- switch (tempString[0]) {
- case 0 : tempString[1] = tempString[2] = '0';
- case 1 : tempString[2] = tempString[1];
- tempString[1] = '0';
- }
- tempString[0] = 2;
- }
-
- /*------------------------------------------------------------------------------*/
-
- pascal void SetMenuItemText(MenuHandle theMenu,short item,StringPtr itemString)
- {
- SetItem(theMenu, item,itemString);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CopyResource (short RFrom, short RTo, ResType itsType, short itsID)
- /* copy the specified resource from the resource file specified by */
- /* RFrom into the resource file specified by RTo */
- {
- Str255 itsName;
- Handle HTemp = NULL;
- short RCurrent = CurResFile();
-
- UseResFile (RFrom);
- HTemp = Get1Resource (itsType, itsID);
- if (HTemp)
- {
- GetResInfo (HTemp, &itsID, &itsType, itsName);
- DetachResource (HTemp);
- UseResFile(RTo);
- AddResource (HTemp, itsType, itsID, itsName);
- WriteResource (HTemp);
- }
-
- UseResFile (RCurrent);
- }
-
-